/*
 * 
 */
package com.yuanmei.stockeyes.charts;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnTouchListener;

import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;
import com.yuanmei.stockeyes.R;
import com.yuanmei.stockeyes.artifacts.Extensions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.limc.androidcharts.entity.DateValueEntity;
import cn.limc.androidcharts.entity.LineEntity;
import cn.limc.androidcharts.event.IDisplayCursorListener;
import cn.limc.androidcharts.view.SlipMinusLineChart;

//import android.util.Log;

public class TalibCciChart implements ITalibChart {
    private final Core mTaLibCore = new Core();
    private Context mContext;
    private SlipMinusLineChart mCciChartView;
    private static final String TAG = TalibCciChart.class.getName();

    private List<DateValueEntity> mCciDateValueList;

    private LineEntity<DateValueEntity> mCciLineEntity;

    private int mCciSmaPeriod = 14;
    private String mCciSmaPeriodName;

    private int mTaIdxDataSetsBegin;
    private int mTaIdxDataSetsNumber;

    public TalibCciChart(Context context) {
        mCciChartView = new SlipMinusLineChart(context);
        TalibChartFactory.commonInitGridChart(mCciChartView);
        TalibChartFactory.commonInitSlipLineChart(mCciChartView);

        mContext          =  mCciChartView.getContext();
        mCciSmaPeriodName = mContext.getString(R.string.param_cci_sma_period);
    }

    @Override
    public List<Map<String, Object>> getTaParamsList() {
        List<Map<String, Object>> outParamsList = new ArrayList<Map<String, Object>>();
        Map<String, Object> item = new HashMap<String, Object>();
        item.put(ITalibChart.ITaParamNameKey,  mCciSmaPeriodName);
        item.put(ITalibChart.ITaParamValueKey, mCciSmaPeriod);
        outParamsList.add(item);

        return outParamsList;
    }

    @Override
    public void setTaParams(Hashtable<String, Integer> inParamMaps) {
        if (inParamMaps.containsKey(mCciSmaPeriodName)
                ) {
            mCciSmaPeriod = inParamMaps.get(mCciSmaPeriodName);
        }
    }

    @Override
    public boolean reloadData(int dataLen,
                              float[] openPrices,
                              float[] highPrices,
                              float[] lowPrices,
                              float[] closePrices,
                              float[] volumes,
                              float[] amounts,
                              int[] dates) {
        MInteger outBegIdx    = new MInteger();
        MInteger outNBElement = new MInteger();
        double[] outCCI       = new double[dataLen];

     // cci(int startIdx, int endIdx,
     //                  float[] inHigh, float[] inLow, float[] inClose,
      //                 int optInTimePeriod, MInteger outBegIdx, MInteger outNBElement, double[] outReal) 
        RetCode retCode = mTaLibCore.cci(0, closePrices.length - 1,
                highPrices, lowPrices, closePrices, 
                mCciSmaPeriod,
                outBegIdx, outNBElement, outCCI);
        if (retCode != RetCode.Success) {
            Log.e(TAG, "CCI Chart calc error retCode=" + retCode);
            return false;
        }

        mCciDateValueList = new ArrayList<>();
        for (int i = 0; i < dataLen; i++) {
            if (i < outBegIdx.value) {
                mCciDateValueList.add(new DateValueEntity(
                        (float) (outCCI[0]), dates[i]));
            } else {
                int idx = i - outBegIdx.value;
                mCciDateValueList.add(new DateValueEntity(
                        (float) (outCCI[idx]), dates[i]));
            }
        }

        List<LineEntity<DateValueEntity>> lines = new ArrayList<LineEntity<DateValueEntity>>();

        mCciLineEntity = new LineEntity<DateValueEntity>();
        mCciLineEntity.setTitle("CCI");
        mCciLineEntity.setLineColor(Color.WHITE);
        mCciLineEntity.setLineData(mCciDateValueList);
        lines.add(mCciLineEntity);

        mCciChartView.setLinesData(lines);

        mTaIdxDataSetsBegin  = outBegIdx.value;
        mTaIdxDataSetsNumber = outNBElement.value;
        return true;
    }

    @Override
    public int getTaIndexNumber() {
        return mTaIdxDataSetsNumber;
    }

    @Override
    public String getTaIndexParms() {
        return   "" + mCciSmaPeriod;
    }

    @Override
    public String getTaIndexShortName() {
        return "CCI";
    }

    @Override
    public Spanned getTaIndexDetail() {
        if (null == mCciDateValueList) {
            return new SpannableString("----");
        }
        int index = mCciChartView.getSelectedIndex();

        DateValueEntity cciEntity = mCciDateValueList.get(index);
        String          cciText   = String.format(Locale.ENGLISH, "CCI:%.2f ",
                cciEntity.getValue());
        String          dateText  = String.format(Locale.ENGLISH, "%d ", cciEntity.getDate());

        String    finalString = cciText + dateText;
        Spannable sb          = new SpannableString(finalString);

        sb.setSpan(new ForegroundColorSpan(mCciLineEntity.getLineColor()), finalString.indexOf(cciText),
                finalString.indexOf(cciText) + cciText.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.GRAY), finalString.indexOf(dateText),
                finalString.indexOf(dateText) + dateText.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

        return (sb);
    }

    @Override
    public int getTaIndexBegin() {
        return mTaIdxDataSetsBegin;
    }

    @Override
    public View getChartView() {
        return mCciChartView;
    }

    @Override
    public void setDisplayFrom(int from) {
        mCciChartView.setDisplayFrom(from);
    }

    @Override
    public void setDisplayCrossXOnTouch(boolean displayCrossXOnTouch) {
        mCciChartView.setDisplayCrossXOnTouch(displayCrossXOnTouch);
    }

    @Override
    public void setDisplayNumber(int number) {
        mCciChartView.setDisplayNumber(number);
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        mCciChartView.setOnTouchListener(l);
    }

    @Override
    public void setOnDisplayCursorListener(
            IDisplayCursorListener l) {
        mCciChartView.setOnDisplayCursorListener(l);
    }

    @Override
    public PointF getTouchPoint() {
        return mCciChartView.getTouchPoint();
    }

    @Override
    public void setTouchPoint(PointF touchPoint) {
        mCciChartView.setTouchPoint(touchPoint);
    }

    @Override
    public void postInvalidate() {
        mCciChartView.postInvalidate();
    }

    @Override
    public float getValidMiddleY() {
        return mCciChartView.getValidMiddleY();
    }
}
